package com.mrd.frame.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayDataDataserviceBillDownloadurlQueryModel;
import com.alipay.api.domain.AlipayTradeCancelModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.mrd.frame.domain.AccessConfig;
import com.mrd.frame.domain.params.PayParams;
import com.mrd.frame.onlinepay.AliPayApi;
import com.mrd.frame.service.AccessConfigService;

/**
 * 支付宝平台请求
 * @author han
 */
@Controller
@RequestMapping("/alipay")
public class AlipayController extends AliPayApi{
	@Autowired
	AccessConfigService accessConfigService;
	@Value("${pay.airpay.timeoutExpress}")
	public String timeoutExpress;//支付超时
	
	/**
	 * 扫码支付
	 */
	@ResponseBody
	@RequestMapping("/tradePrecreatePay")
	public String tradePrecreatePay(@ModelAttribute PayParams params) {
		String subject = "Javen 支付宝扫码支付测试";
		String storeId = "123";
		String notifyUrl = "";
		String qr_code = "";
		AccessConfig accessConfig = accessConfigService.selectByUserKeyAndPayChannel(params.getUserKey(), "2");
		notifyUrl = accessConfig.getNotifyUrl();
		AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
		model.setSubject(params.getSubject());
		model.setTotalAmount(params.getTotalAmount());
		model.setStoreId(storeId);
		model.setTimeoutExpress(timeoutExpress);
		model.setOutTradeNo(params.getOutTradeNo());
		try {
			String resultStr = tradePrecreatePay(model, notifyUrl,params.getUserKey());
			JSONObject jsonObject = JSONObject.parseObject(resultStr);
			qr_code = jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return qr_code;
	}
	
	/**
	 * 交易查询
	 * @author han
	 */
	@ResponseBody
	@RequestMapping("/tradeQuery")
	public boolean tradeQuery(String userKey,String outTradeNo) {
		boolean isSuccess = false;
		try {
			AlipayTradeQueryModel model = new AlipayTradeQueryModel();
			model.setOutTradeNo(outTradeNo);
//			model.setTradeNo("2017042521001004200200236814");

			isSuccess = isTradeQuery(model,userKey);
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return isSuccess;
	}

	/**
	 * 交易查询订单，并且返回订单信息
	 * @author han
	 */
	@ResponseBody
	@RequestMapping("/tradeQueryByStr")
	public String tradeQueryByStr(String userKey,String outTradeNo) {
		String resultStr = "";
		AlipayTradeQueryModel model = new AlipayTradeQueryModel();
		model.setOutTradeNo(outTradeNo);

		try {
			AlipayTradeQueryResponse queryResponse = tradeQuery(model,userKey);
			resultStr = queryResponse.getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return resultStr;
	}
	
	/**
	 * 下载对账单
	 * @author han
	 */
	@ResponseBody
	@RequestMapping("/dataDataserviceBill")
	public String dataDataserviceBill(String billDate,String userKey) {
		String resultStr = "";
		try {
			AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel();
			model.setBillType("trade");
			model.setBillDate(billDate);
			resultStr = billDownloadurlQuery(model,userKey);
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return resultStr;
	}

	/**
	 * 退款
	 * @author han
	 */
	@ResponseBody
	@RequestMapping("/tradeRefund")
	public void tradeRefund(String userKey,String outTradeNo,String tradeNo,
			String refundAmount,String refundReason) {
		String resultStr = "";
		try {
			AlipayTradeRefundModel model = new AlipayTradeRefundModel();
//			model.setOutTradeNo("042517111114931");
//			model.setTradeNo("2017042521001004200200236813");
//			model.setRefundAmount("86.00");
//			model.setRefundReason("正常退款");
			model.setOutTradeNo(outTradeNo);
			model.setTradeNo(tradeNo);
			model.setRefundAmount(refundAmount);
			model.setRefundReason(refundReason);
			resultStr = tradeRefund(model,userKey);
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 撤销订单
	 * @author han
	 */
	@ResponseBody
	@RequestMapping("/tradeCancel")
	public boolean tradeCancel(String userKey,String outTradeNo,String tradeNo) {
		boolean isSuccess = false;
		try {
			AlipayTradeCancelModel model = new AlipayTradeCancelModel();
//			model.setOutTradeNo("042518024814931");
//			model.setTradeNo("2017042521001004200200236814");
			model.setOutTradeNo(outTradeNo);
			model.setTradeNo(tradeNo);
			isSuccess = isTradeCancel(model,userKey);
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return isSuccess;
	}
}
